var numi = 1;
let pages = 0;
let page = 0;
var fenyearr = "";
var Department = JSON.parse(localStorage.getItem("Department"));
$(document).ready(function(){
    initialize(Department);
})
$(".sear").click(function (e) { 
    e.preventDefault();
    let str = $(".sear_tiao").val();
    searDep(str);
});
$(".bu").change(function (e) { 
    e.preventDefault();
    let str = $(".bu").val();
    searDep1(str);
});
$("#addq").click(function (e) { 
    e.preventDefault();
    let name = $(".depna").val();
    let depre = $(".depre").val();
    if(name == "" ||depre == "") {

    }else {
        addDep(name,depre,Department);
    }
    $("#depAdd").modal('hide');
    $(".depna").val("");
    $(".depre").val("");
});
$("#delq").click(function (e) { 
    e.preventDefault();
    Department.splice(numi,1);
    localStorage.setItem("Department", JSON.stringify(Department));
    Department = JSON.parse(localStorage.getItem("Department"));
    $("#depDel").modal('hide');
    initia(Department);
});
$("#upq").click(function (e) { 
    e.preventDefault();
    Department[numi].DepName = $(".dename").val();
    Department[numi].Remark = $(".dename").val();
    localStorage.setItem("Department", JSON.stringify(Department));
    $("#depUp").modal('hide');
    $(".dename").val("");
    $(".dere").val("");
    
    initia(Department);
});
$(".juzhong a").click(function (e) { 
    e.preventDefault();
    num = $(this).parent().val();
    if(page != 1) {
        if(num==98) {
            page--;
            initia(fenyearr);
        }
    }
    if(page != pages) {
        if(num==99) {
            page++
            initia(fenyearr);
        }
    }
});

$('#depDel').on('show.bs.modal', function (event) {
    let numid = parseInt($($(event.relatedTarget).parent().parent().siblings()).html());
    var result = Department.findIndex((v) => {
        return v.DepID === numid;
    });
    numi = result;
    $(".depname").html(Department[result].DepName);
    $(".depre").html(Department[result].Remark);
})
$('#depUp').on('show.bs.modal', function (event) {
    let numid = parseInt($($(event.relatedTarget).parent().parent().siblings()).html());
    var result = Department.findIndex((v) => {
        return v.DepID === numid;
    });
    numi = result;
    console.log(result);
    $(".dename").attr("placeholder",Department[result].DepName);
    $(".dere").attr("placeholder",Department[result].Remark);
})
$('#depShow').on('show.bs.modal', function (event) {
    let numid = parseInt($($(event.relatedTarget).parent().parent().siblings()).html());
    var result = Department.findIndex((v) => {
        return v.DepID === numid;
    });
    console.log(Department[result].DepName);
    $(".depid1").html(Department[result].DepID);
    $(".depidname1").html(Department[result].DepName);
    $(".depnum").html(Department[result].num);
    $(".depire1").html(Department[result].Remark);
    $(".depfz1").html(Department[result].fuZeRenname);
    $(".deptime").html(Department[result].times);
})



//初始化加载
function initialize(arr1) {
    let arr = fenye(arr1);
    fenyearr = arr1;
    for(let i = 0; i<arr.length; i++) {
        let str = `<option>${arr[i].DepName}</option>`;
        let str1 = `<tr>
                        <td>${arr[i].DepID}</td>
                        <td>${arr[i].DepName}</td>
                        <td>${arr[i].Remark}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".bu").append(str);
        $(".dep_table").append(str1);
    }
}
//二次加载
function initia(arr1) {
    $(".biaotou").siblings().remove();
    let arr = fenye(arr1);
    fenyearr = arr1;
    for(let i = 0; i<arr.length; i++) {
        let str1 = `<tr>
                        <td>${arr[i].DepID}</td>
                        <td>${arr[i].DepName}</td>
                        <td>${arr[i].Remark}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow"
                                id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".dep_table").append(str1);
    }
}
//新增
function addDep(depName,depRem,arr){
    let timedate = new Date;
    let str = `${timedate.getFullYear()}-${timedate.getMonth()+1}-${timedate.getDate()}`;
    var add = {
        DepID : arr.length+1,
        DepName : depName,
        Remark : depRem,
        fuZeRenname: "暂无",
        num: 0,
        times:`${str}`
    }
    arr.push(add);
    console.log(arr);
    localStorage.setItem("Department", JSON.stringify(arr));
    Department = JSON.parse(localStorage.getItem("Department"));
    initia(Department);
    // location.reload();
}
//删除
function delDep() {

}
//修改
function upDep() {

}
//查看
function showDep() {

}
//搜索
function searDep(str) {
    let arr1 = [];
    Department = JSON.parse(localStorage.getItem("Department"));
    if(str != "") {
        var result = Department.findIndex((v) => {
            return v.DepName === str;
        });
        if(result == -1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        } else {
            arr1.push(Department[result]);
            initia(arr1);
        }
    }else {
        if(Department.length == $(".dep_table tbody").children().length-1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        }else {
            location.reload();
        }
    }
}
function searDep1(str) {
    Department = JSON.parse(localStorage.getItem("Department"));
    let arr1 = [];
    if(str == "全部") {
        location.reload();
    }else {
        var result = Department.findIndex((v) => {
            return v.DepName === str;
        });
        arr1.push(Department[result]);
        initia(arr1);
    }
}
function fenye (arr) {
    let xia  = $(".xia").clone(true);
    $(".shang").nextAll().remove();
    $(".shang").after(xia);
    let num = 0;
    if(arr.length<=6) {
        pages = 1
        num = arr.length;
    }else {
        pages = parseInt(arr.length/6);
        num = 6;
        if(arr.length%6 != 0) {
            pages += 1;
            
        }
    }
    for (let i = 1; i <= pages; i++) {
        $(".xia").before(`<li class= ab,a${i} value = ${i}><a onclick = "dijifenye(this)">${i}</a></li>`);
    }
    if(page == 0) {
        $(".a1").addClass("active");
        page=1;
    }else {
        $(`.a${page}`).addClass("active");
        
    }
    if(page == 1) {
        $(".shang").addClass("disabled");
    }else {
        $(".shang").removeClass("disabled")
    }
    if(page == pages) {
        $(".xia").addClass("disabled");
    }else {
        $(".xia").removeClass("disabled")
    }
    let arr1 = arr.concat();

    return arr1.splice(page*6-6,num);
}
function dijifenye(num) {
    page = parseInt($(num).html());
    initia(fenyearr);
}



